package com.jd.pet.controller;

import com.jd.pet.dto.OrderDto;
import com.jd.pet.service.ISearchMasterMsgService;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.query.SearchMasterMsgQuery;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){ 
        if( searchMasterMsg.getId()!=null){
		searchMasterMsgService.update(searchMasterMsg);
        }else{
                searchMasterMsgService.add(searchMasterMsg);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        searchMasterMsgService.del(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        searchMasterMsgService.patchDel(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id")Long id) {
        return searchMasterMsgService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 发布寻主消息
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        return searchMasterMsgService.publish(searchMasterMsg,request);
    }

    /**
     * 前台用户查询寻主消息
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        return searchMasterMsgService.user(searchMasterMsgQuery,request);
    }

    /**
     * 管理员查看待处理寻主消息
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        return searchMasterMsgService.toHandle(searchMasterMsgQuery,request);
    }

    /**
     * 查看寻主池
     * @param searchMasterMsgQuery
     * @param
     * @return
     */
    @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery) {
        return searchMasterMsgService.toSearchMasterPool(searchMasterMsgQuery);
    }

    /**
     * 管理员查看已完成寻主消息
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        return searchMasterMsgService.finish(searchMasterMsgQuery,request);
    }

    /**
     * 拒单
     * @param msgId
     * @return
     */
    @GetMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId) {
        return searchMasterMsgService.reject(msgId);
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody OrderDto orderDto) {
        return searchMasterMsgService.accept(orderDto);
    }

}
